import json
from models import BaseModels
from sqlalchemy import Column, ForeignKey, BigInteger, Integer, String, Date, Boolean


class Tasks(BaseModels):
    __tablename__ = 'tasks'
    id = Column(BigInteger, autoincrement=True, primary_key=True, unique=True, nullable=False)
    orders = Column(String(64), nullable=False, index=True, comment="订单编号")
    name = Column(String(64), nullable=False, index=True, comment="工单名称")
    amount = Column(BigInteger, nullable=False, comment="计划生产数量")
    totals = Column(BigInteger, nullable=False, comment="生产数，包括不合格的")
    actual = Column(BigInteger, nullable=False, default=0, comment="实际生产数量，从头到尾， 合格的")
    aircraft_id = Column(Integer, ForeignKey("aircraft.id"), index=True, comment="绑定机种数据库ID")
    time = Column(Date, nullable=False, comment="计划生产时间")
    delivery = Column(Date, nullable=True, comment="生产完成时间, MES与PLC交互")
    status = Column(Boolean, nullable=False, comment="订单生产状态")

    def __repr__(self):
        return json.dumps({
            "task_id": self.id,
            "orders": self.orders,
            "name": self.name,
            "amount": self.amount,
            "totals": self.totals,
            "actual": self.actual,
            "time": self.time.strftime("%Y-%m-%d"),
            "delivery": self.delivery.strftime("%Y-%m-%d") if self.delivery else None
        })
